define(['app'], function(app) {
    'use strict';

    var controller = function controller($scope, Activity, $stateParams, $window, $timeout, $ionicSlideBoxDelegate, GoPage,$interval,Auth,ChatInfo) {
       
        $scope.selectIndex = 0;
    	$scope.status = "started";//默认进行中
        $scope.datasArr = new Array();//存放一览数据
		$scope.nPageArr = [1,1];//页数
		
       //画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			$scope.lan = 0; //这是状态栏，只有ios的APP才有
			if(window.cordova && window.cordova.plugins && ionic.Platform.isIOS()) {
				$scope.lan = 20;
			}
			$scope.all = $window.innerHeight;
			getMethod();//必须在data_init()之前
			$scope.data_init();
		}, false);
		
		$scope.$on('$ionicView.beforeLeave',function(){
			$interval.cancel($scope.timer);
		})
		
        //切换页面
		$scope.activeSlide = function(index) {
			$scope.selectIndex = index;
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide($scope.selectIndex);
			if($scope.selectIndex==1){
				$scope.status = "will";
			}else{
				$scope.status = "started";
			}
			if(!$scope.datasArr[$scope.selectIndex]){
				getList();
			}
		};
		$scope.slideChanged = function(index) {
			$scope.selectIndex = index;
		};
		
		//禁止左右滑动来切换页面1
		$scope.onTouch = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(false);
		};
		//禁止左右滑动来切换页面2
		$scope.onRelease = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(true);
		};
		
		/*
		 * 9个活动
		 * 活动类型
			1=>团购活动
			2=>秒杀活动
			3=>限时抢购活动
			4=>拍卖活动
			5=>新品测试活动
			6=>新品赠送活动
			7=>新品公告活动
			8=>产品推广活动(直播)
			9=>产品促销活动
			有的活动有三级页面，有的只有二级
		*/
		function getMethod(){
			$scope.from_type = "";//8和9用到这个参数
			if($stateParams.type=='1') {//groupbuy.html
				$scope.title = "疯狂团购";
				$scope.detailUrl = "activitysecond";//groupbuydetail
				$scope.method = "marketing.groupbuy.viewlist";
			}
			if($stateParams.type=='2') {//seckill.html
				$scope.title = "限时秒杀";
				$scope.detailUrl = "activitysecond";//seckilldetail
				$scope.method = "marketing.seckill.viewlist";
			}
			if($stateParams.type=='3') {//panicbuying.html
				$scope.title = "限时抢购";
				$scope.detailUrl = "activitysecond";//panicbuyingdetail.html(activitySecond.html由该页面改造而来)
				$scope.method = "marketing.panicbuy.viewlist";
			}
			if($stateParams.type=='5'){//newtest.html(activity.html由该页面改造而来)
				$scope.title = "新品测试";
				$scope.detailUrl = "newtest_detail";
				$scope.method = "goods.test.getlist";
			}
			if($stateParams.type=='6'){//newgive.html
				$scope.title = "新品赠送";
				$scope.detailUrl = "newsend_detail";
				$scope.method = "marketing.giving.goodslist";
			} 
			if($stateParams.type=='7') {
				$scope.title = "新品公告";
				$scope.detailUrl = "newnotice_detail";
				$scope.method = "marketing.notice.getlist";
			}
			if($stateParams.type=='8') {//参数：media。promotions.html
				$scope.title = "直播";
				$scope.from_type = "media";
				$scope.detailUrl = "activitysecond";//promotionslive
				$scope.method = "marketing.promotion.viewlist";
				$scope.get_loginfo();
			}
			if($stateParams.type=='9'){//参数：image。promotionslist.html。参数act_type和from_type是反过来的，周一让接口改一下
				$scope.title = "商品促销";
				$scope.from_type = "image";
				$scope.detailUrl = "activitysecond";//promotionspicture
				$scope.method = "marketing.promotion.viewlist";
				$scope.get_loginfo();
			}
		}
		
		//初始化活动
		function getList(){
			Activity.getList(1, $scope.status,$scope.method,$scope.from_type, function(data, now_time) {
				if(data != null) {
					$scope.init_time(data, now_time);
					$scope.datasArr[$scope.selectIndex] = data;
					$scope.set_timer(now_time,data.act);
				} else {
					$scope.datasArr[$scope.selectIndex] = new Object();
				}
			});
		}
		
		
		//初始化页面
		$scope.data_init = function() {
			
			getList();
			
			//轮播图
			Activity.getBannerList($stateParams.type,function(data){
//				console.log(data.result)
				if(data.result&&data.result.associate){
					$scope.banners = data.result.associate;
					$ionicSlideBoxDelegate.$getByHandle('advs-handle').update(); //解决无法加载
					$ionicSlideBoxDelegate.$getByHandle('advs-handle').loop(true); //循环
					$timeout(function(){//获取轮播图的高度，获取到的高度一般是最高的那张图片的高度
						var advsBox = document.getElementById("advsBox").clientHeight;
						$scope.myObj = {
							"height": $scope.all - 44 - 35 - advsBox - $scope.lan + "px"
						}
					},500)
				}else{
					$scope.myObj = {
						"height": $scope.all - 44 - 35 - $scope.lan + "px"
					}
				}
			})
		}
		
		//跳转到活动详情
		$scope.onshow_detail= function(act_id,goods_id,product_id,as_id) {
			GoPage.go_states($scope.detailUrl, {
				"act_id": act_id,
				"goods_id":goods_id,
				"product_id":product_id,
				'as_id':as_id,//8和9用到
				"type":$stateParams.type
			});
		};
		
		//小小图登录信息
		$scope.get_loginfo = function() {
			//判断本地是否保存有小小图登录信息
			var is_chatLogin = ChatInfo.get_isInfos();
			if(is_chatLogin == false && Auth.check_loginInfo())
				Activity.get_loginfo(function(data) {
					$scope.chat_url = data.chat_url;
					$scope.login_code = data.login_code;
					$scope.soket_url = data.soket_url;
					//初次保存小小图登录信息
					ChatInfo.save_chat_loginInfo($scope.chat_url, $scope.login_code, $scope.soket_url);
				})
		};
		
		/******************时间处理**********************/
		$scope.init_time = function(data, now_time) {
			for(var i = 0; i < data.act.length; i++) {
				if(data.act[i].now_time != undefined)
					data.act[i].now_time = data.act[i].now_time - 1;
				else{
					if(data.act_status==1){
						data.act[i].now_time = data.act[i].start_time - now_time;
					}else{
						data.act[i].now_time = data.act[i].end_time - now_time;
					}
				}
				data.act[i].formate_time = $scope.formate_time(data.act[i].now_time);
			}
		}
		$scope.set_timer = function(now_time,dataArr) {
			if(!dataArr||$scope.status == "will") return;
		  	$scope.timer = $interval(function() {
				for(var i = 0; i < dataArr.length; i++) {
					if(dataArr[i].now_time != undefined)
						dataArr[i].now_time = dataArr[i].now_time - 1;
					else
						dataArr[i].now_time = dataArr[i].end_time - now_time;
					dataArr[i].formate_time = $scope.formate_time(dataArr[i].now_time);
				}
			}, 1000)
		}
		$scope.formate_time = function(time) {
			if(time > 0) {
				var day = parseInt(time / (3600 * 24));
				var hour = parseInt((time % (3600 * 24)) / 3600);
				if(hour < 10)
					hour = "0" + hour;
				var min = parseInt((time % 3600) / 60);
				if(min < 10)
					min = "0" + min;
				var second = time % 60;
				if(second < 10)
					second = "0" + second;
				return day + '天' + hour + '小时' + min + '分' + second + '秒';
			} else {
				return '0天0小时0分0秒';
			}
		}
		/******************时间处理**********************/
		
		
		/*****************上下拉刷新*****************/
		$scope.isLoad = false;
		$scope.doRefresh = function() {
			$scope.isLoad = true;
			Activity.getList(1, $scope.status, $scope.method,$scope.from_type,function(data, now_time) {
				if(data != null) {
					$scope.init_time(data, now_time);
					$scope.datasArr[$scope.selectIndex] = data;
					$scope.set_timer(now_time,data.act);
				} else {
					$scope.datasArr[$scope.selectIndex] = new Object();
				}
				$scope.nPageArr[$scope.selectIndex] = 1;
			}, function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			})
		} 
		
		$scope.doRefreshMore = function() {
			$scope.isLoad = true;
			Activity.getList($scope.nPageArr[$scope.selectIndex]+1, $scope.status,$scope.method,$scope.from_type, function(data, now_time) {
//				console.log(data)
				console.log($scope.nPageArr)
				if(data != null&&data.act) {
					if($scope.datasArr[$scope.selectIndex].act != undefined) {
						$scope.init_time(data, now_time);
						$scope.datasArr[$scope.selectIndex].act = $scope.datasArr[$scope.selectIndex].act.concat(data.act);
						$scope.nPageArr[$scope.selectIndex] += 1;
					} else {
						$scope.datasArr[$scope.selectIndex] = data;
					}
				}
			}, function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			})
		}
		
		//跳转到店铺首页
		$scope.onshow_first = function(store_id,$event){
			$event.stopPropagation();//阻止事件冒泡
			GoPage.go_states("first", {
				'store_id': store_id
			});
		}
		

    }

    controller.$inject = ['$scope', 'Activity', '$stateParams', '$window', '$timeout', '$ionicSlideBoxDelegate', 'GoPage','$interval','Auth','ChatInfo'];

    return app.registerController("activityctrl",controller);
});